package com.android.todayview;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class TodayView extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Configuration app_config = getResources().getConfiguration();
		/** If orientation is port */
		if(app_config.orientation==Configuration.ORIENTATION_PORTRAIT)
			setContentView(R.layout.login_port);
		/** If orientation is landscope */
		else if(app_config.orientation==Configuration.ORIENTATION_LANDSCAPE)
			setContentView(R.layout.login_land);
		/** Get widget ID */
		GetWidgetID();
		/** Set button submit listener */
		SetListener();
	}

	/** Declare widgets we have used in login interface */
	private Button button_submit;
	private Button button_website;
	private EditText edittext_username;
	// private EditText edittext_password;
	
	private final int CKECK_OUT = 1;

	/** Get widget object information by ID */
	private void GetWidgetID() {
		button_submit = (Button) this.findViewById(R.id.submit);
		button_website = (Button) this.findViewById(R.id.website);
		edittext_username = (EditText) this.findViewById(R.id.username);
		// edittext_password = (EditText) this.findViewById(R.id.password);
	}
	
	/** Declare submit button click listener */
	private Button.OnClickListener submit_listener = new Button.OnClickListener() {
		public void onClick(View v) {
			/** When submit_button is clicked, something will happen here */
			//if ((edittext_username.getText().toString() == "1")
			//		|| (edittext_password.getText().toString() == "2")) {
			if(true) {
				/** If username and password is correct */
				/** Switch to mainface */
				/** Declare a new intent */
				Intent intent_tabview = new Intent();
				/** Define new intent with source and destination class */
				intent_tabview.setClass(TodayView.this, TabView.class);
				/** Declare a bundle */
				Bundle bundle_tabview = new Bundle();
				
				bundle_tabview.putInt("type",CKECK_OUT);
				/** Bundle with some string information */
				bundle_tabview.putString("KEY_USERNAME", edittext_username.getText().toString());
				/** Integrate bundle to intent */
				intent_tabview.putExtras(bundle_tabview);
				/** Start a new activity */
				startActivity(intent_tabview);
			} //else {
				/** If username and password is uncorrect */
				// Toast.makeText(TodayView.this, R.string.login_incorrect_info,10).show();
			//}
		}
	};

	/** Declare website button click listener */
	private Button.OnClickListener website_listener = new Button.OnClickListener() {
		public void onClick(View v) {
			/** When about_button is clicked, something will happen here */
			/** Get string from resource by getString function */
			Uri website_uri = Uri.parse(getString(R.string.login_website_uri));
			Intent intent = new Intent(Intent.ACTION_VIEW, website_uri);
			startActivity(intent);
		}
	};

	/** Set widgets monitor */
	private void SetListener() {
		button_submit.setOnClickListener(submit_listener);
		button_website.setOnClickListener(website_listener);
	}
	
	/** Create menu for this activity */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater login_menuinflater = getMenuInflater();
		login_menuinflater.inflate(R.menu.menu_login, menu);
		return super.onCreateOptionsMenu(menu);
	}

	/** Menu action */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId())
		{
		case R.id.login_menu_about:
			AboutInfo();
			break;
		case R.id.login_menu_exit:
			TodayView.this.finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	/** Click about button to get some information from about dialog */
	void AboutInfo() {
		Builder about_builder = new AlertDialog.Builder(TodayView.this);
		about_builder.setTitle(R.string.about_title);
		about_builder.setMessage(R.string.about_information);
		// about_builder.setPositiveButton(R.string.about_ok, );
		about_builder.show();
	}

}
